<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js - WebGPU - Skinning</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGPU - Skinning<br />
			(Chrome Canary with flag: --enable-unsafe-webgpu)
		</div>

		<script type="importmap">
		{
			"imports": {
				"three": "../build/three.module.js"
			}
		}
		</script>
		<script type="module">

			import * as THREE from 'three';

			import { FBXLoader } from './jsm/loaders/FBXLoader.js';

			import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
			import WebGPU from './jsm/renderers/webgpu/WebGPU.js';

			import LightsNode from './jsm/renderers/nodes/lights/LightsNode.js';

			let camera, scene, renderer;

			let mixer, clock;

			init().then( animate ).catch( error );

			async function init() {

				if ( WebGPU.isAvailable() === false ) {

					document.body.appendChild( WebGPU.getErrorMessage() );

					throw 'No WebGPU support';

				}

				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.set( 100, 200, 300 );

				scene = new THREE.Scene();
				camera.lookAt( 0, 100, 0 );

				clock = new THREE.Clock();

				//lights

				const light = new THREE.PointLight( 0xffffff );
				camera.add( light );
				scene.add( camera );

				const lightNode = LightsNode.fromLights( [ light ] );

				const loader = new FBXLoader();
				loader.load( 'models/fbx/Samba Dancing.fbx', function ( object ) {

					mixer = new THREE.AnimationMixer( object );

					const action = mixer.clipAction( object.animations[ 0 ] );
					action.play();

					object.traverse( function ( child ) {

						if ( child.isMesh ) {

							child.material = new THREE.MeshStandardMaterial();
							child.material.lightNode = lightNode;

						}

					} );

					scene.add( object );

				} );

				//renderer

				renderer = new WebGPURenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize );

				return renderer.init();

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );

				const delta = clock.getDelta();

				if ( mixer ) mixer.update( delta );

				renderer.render( scene, camera );

			}

			function error( error ) {

				console.error( error );

			}

		</script>
	</body>
</html>
